package com.blockmeta.bbs.immodule.widget.msgview;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.blockmeta.bbs.businesslibrary.net.pojo.BaseCodeResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.bean.HongbaoDetail;
import com.blockmeta.bbs.immodule.bean.Params;
import com.blockmeta.bbs.immodule.bean.databean.HongBaoContentBean;
import com.blockmeta.bbs.immodule.dao.ChatMessage;
import com.blockmeta.bbs.immodule.net.api.GetHongbaoApi;
import com.blockmeta.bbs.immodule.widget.Popup_Qiang_Hongbao;
import com.chad.library.adapter.base.BaseViewHolder;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

public class HongbaoMsgView<T extends ChatMessage> extends MsgView<T>{

    public HongbaoMsgView(Context context) {
        super(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.im_chat_hongbao;
    }

    @Override
    public void convert(BaseViewHolder helper, final T item, T lastItem) {
        super.convert(helper, item, lastItem);
        super.bindCommonControl(helper, item, lastItem);

        final HongBaoContentBean contentBean = getContentBean(item, HongBaoContentBean.class);
        String deepLink = "hongbao";
        String linkText = "";
        if(contentBean.getType() == Params.HONGBAO_LUCKY){
            linkText = "拼手气红包";
        }else if(contentBean.getType() == Params.HONGBAO_EQUAL){
            linkText = "均分红包";
        }else if(contentBean.getType() == Params.HONGBAO_PERSON){
            linkText = "个人红包";
        }
        //发送消息！
        if (item.getType() == ChatMessage.TYPE_SEND) {
            ImageView icon = helper.getView(R.id.right_img_icon);
//            LoadPicUtil.loadWithPicasso(icon, contentBean.getImg());
            helper.setText(R.id.right_title, contentBean.getTitle());
            helper.setText(R.id.right_link, linkText);
            View contentLayout = helper.getView(R.id.chat_item_layout_content);
            contentLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    clickHongbaoMsg(item, contentBean);

                }
            });
//            contentLayout.setTag(deepLink);
//            helper.addOnClickListener(R.id.chat_item_layout_content);
        } else {
            //接收消息
            ImageView icon = helper.getView(R.id.left_img_icon);
//            LoadPicUtil.loadWithPicasso(icon, contentBean.getImg());
            helper.setText(R.id.left_title, contentBean.getTitle());
            helper.setText(R.id.left_link, linkText);
            View contentLayout = helper.getView(R.id.left_chat_item_layout_content);
            contentLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    clickHongbaoMsg(item, contentBean);

                }
            });
//            contentLayout.setTag(deepLink);
//            helper.addOnClickListener(R.id.left_chat_item_layout_content);
        }

    }

    private void clickHongbaoMsg(final T item, HongBaoContentBean contentBean) {
        Log.i("author", "clickhongbao" + item.toString());

        RetrofitFactory.getRetrofit().create(GetHongbaoApi.class)
                .getCandyBoxDetail(contentBean.getId())
                .compose(RxUtil.<BaseCodeResponse<HongbaoDetail>>rxSchedulerHelper())
                .compose(RxUtil.<HongbaoDetail>commonResultByCode())
                .compose(((RxAppCompatActivity) mContext).<HongbaoDetail>bindToLifecycle())
                .subscribe(new CommonSubscriber<HongbaoDetail>(mContext) {
                    @Override
                    public void onNext(HongbaoDetail detail) {
                        Log.i("author", "clickhongbao result" + detail.toString());
                        Popup_Qiang_Hongbao popup_qiang_hongbao = new Popup_Qiang_Hongbao(mContext, detail, item);
                        popup_qiang_hongbao.showPop();
                    }
                });
    }

}
